package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.model.mongo.Friend;
import com.tanhua.model.mongo.UserLike;
import com.tanhua.model.mongo.Visitors;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

import java.util.HashMap;
import java.util.Map;

@DubboService
public class UserLikeApiImpl implements UserLikeApi{

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Boolean saveOrUpdate(Long userId, Long likeUserId, boolean isLike) {
        try {
            //1、查询数据
            Query query = Query.query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId));
            UserLike userLike = mongoTemplate.findOne(query, UserLike.class);
            //2、如果不存在，保存
            if(userLike == null) {
                userLike = new UserLike();
                userLike.setUserId(userId);
                userLike.setLikeUserId(likeUserId);
                userLike.setCreated(System.currentTimeMillis());
                userLike.setUpdated(System.currentTimeMillis());
                userLike.setIsLike(isLike);
                mongoTemplate.save(userLike);
            }else {
                //3、更新
                Update update = Update.update("isLike", isLike)
                        .set("updated",System.currentTimeMillis());
                mongoTemplate.updateFirst(query,update,UserLike.class);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 互相喜欢、喜欢、粉丝、谁看过我 - 翻页列表
     */
    @Override
    public List<Long> findIdsByIds(Integer page, Integer pagesize, Long userId, String type) {
        //相互关注
        if (type.equals("1")){
            Query query = Query.query(
                    Criteria.where("userId").is(userId))
                    .skip((page-1)*pagesize).limit(pagesize);
            List<Friend> users = mongoTemplate.find(query, Friend.class);
            List<Long> friendIds = CollUtil.getFieldValues(users, "friendId", Long.class);
            return friendIds;
        }else if (type.equals("2")){  //喜欢
            Query query = Query.query(Criteria.where("userId").is(userId)
                    .and("isLike").is(true))
                    .skip((page-1)*pagesize).limit(pagesize);
            List<UserLike> users = mongoTemplate.find(query, UserLike.class);
            List<Long> likeUserIds = CollUtil.getFieldValues(users, "likeUserId", Long.class);
            return likeUserIds;
        }else if (type.equals("3")){  //粉丝(被喜欢)
            Query query = Query.query(Criteria.where("likeUserId").is(userId)
                    .and("isLike").is(true))
                    .skip((page-1)*pagesize).limit(pagesize);
            List<UserLike> users = mongoTemplate.find(query, UserLike.class);
            List<Long> fans = CollUtil.getFieldValues(users, "userId", Long.class);
            return fans;
        }else if (type.equals("4")){  //访客
            Query query = Query.query(Criteria.where("userId").is(userId))
                    .skip((page-1)*pagesize).limit(pagesize);
            List<Visitors> users = mongoTemplate.find(query, Visitors.class);
            List<Long> fans = CollUtil.getFieldValues(users,"visitorUserId", Long.class);
            return fans;
        }
        return null;
    }

    /**
     * 取消喜欢
     * @param userId
     * @param disIdL
     */
    @Override
    public void updateByIds(Long userId, Long disIdL) {
        //1、查询数据
        Query query = Query.query(Criteria.where("userId").is(userId)
                .and("likeUserId").is(disIdL)
                .and("isLike").is(true));
        //2、更新
        Update update = Update.update("isLike", false);
        mongoTemplate.updateFirst(query,update,UserLike.class);
    }


    @Override
    public Map counts(Long userId) {

        Query query1 = Query.query(Criteria.where("userId").is(userId));
        Integer eachLoveCount = (int)mongoTemplate.count(query1, Friend.class);

        Query query2 = Query.query(Criteria.where("userId").is(userId).and("isLike").is(true));
        Integer loveCount = (int)mongoTemplate.count(query2, UserLike.class);

        Query query3 = Query.query(Criteria.where("likeUserId").is(userId).and("isLike").is(true));
        Integer fanCount = (int)mongoTemplate.count(query3, UserLike.class);

        Map map = new HashMap();
        map.put("eachLoveCount",eachLoveCount);
        map.put("loveCount",loveCount);
        map.put("fanCount",fanCount);
        return map;
    }
}
